package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.WhsOperateLog;
import com.sunda.spmswms.mapper.WhsOperateLogMapper;
import com.sunda.spmswms.service.IWhsOperateLogService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 库存操作日志表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-04-14
 */
@Service
public class WhsOperateLogServiceImpl extends ServiceImpl<WhsOperateLogMapper, WhsOperateLog> implements IWhsOperateLogService {

    /**
     * WHS_OPERATE_LOG '库存操作日志表'
     * LOG_ID '日志表自增主键ID'
     * DOCUMENT_TYPE '日志单据类型：1自管仓仓库库存日志；2自管仓储位库存日志；3海外仓仓库库存日志；4海外仓储位库存日志；'
     * UUID_STORAGE '储位UUID-储位库存变化时使用'
     * MATERIAL_NO '物料编码'
     * BOX_NOTE '箱码，自管仓备件货出入库、上下架时使用'
     * QUANTITY '数量'
     * OPERATION_TYPE '操作类型：1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)；'
     * DELIVERY_NOTE_SAP '交货单号'
     * RELATE_DOCUMENT_NUMBER '关联单据号(预留)'
     * COMMENTS '操作内容/评论'
     * USER_ID '用户工号'
     * CREATE_TIME '创建时间'
     * REMARK '备注说明'
     */

    /***
     * 1.自管仓 备件货 按箱 验收入库 增加仓库库存 - 日志
     * @param boxNote
     * @param quantity
     * @param deliveryNoteSap
     * @param comments
     * @param userId
     */
    @Override
    public void insertWhsOperateLogBR(String boxNote, double quantity, String deliveryNoteSap,  String relateDocumentNumber, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("1");
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("1");
        if (StringUtils.isNotEmpty(deliveryNoteSap)){
            whsOperateLog.setDeliveryNoteSap(deliveryNoteSap);
        }
        if (StringUtils.isNotEmpty(relateDocumentNumber)){
            whsOperateLog.setRelateDocumentNumber(relateDocumentNumber);
        }
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /**
     * 2.自管仓 备件货 按箱 上架 增加储位库存 - 日志
     *
     * @param uuidStorage
     * @param boxNote
     * @param quantity
     * @param deliveryNoteSap
     * @param comments
     * @param userId
     */
    @Override
    public void insertWhsOperateLogBS(String uuidStorage, String boxNote, double quantity, String deliveryNoteSap, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("2");
        whsOperateLog.setDeliveryNoteSap(deliveryNoteSap);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /***
     * 3.自管仓 贸易货 按物料 上架 增加储位库存 - 日志
     * @param uuidStorage
     * @param materialNo
     * @param quantity
     * @param deliveryNoteSap
     * @param comments
     * @param userId
     */
    @Override
    public void insertWhsOperateLogMS(String uuidStorage, String materialNo, double quantity, String deliveryNoteSap, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setMaterialNo(materialNo);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("2");
        whsOperateLog.setDeliveryNoteSap(deliveryNoteSap);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /**
     * 5.自管仓 备件货 按箱 下架 减少储位库存 - 日志
     *
     * @param uuidStorage
     * @param boxNote
     * @param quantity
     * @param whsOutUuid
     * @param comments
     * @param userId
     */
    @Override
    public void insertWhsOperateLogBX(String uuidStorage, String boxNote, double quantity, String whsOutUuid, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("3");
        whsOperateLog.setRelateDocumentNumber(whsOutUuid);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    @Override
    public void insertWhsOperateLogBX(String uuidStorage, String boxNote, double quantity, String whsOutUuid, String comments, String userId, String remarks, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("3");
        whsOperateLog.setRelateDocumentNumber(whsOutUuid);
        whsOperateLog.setComments(comments);
        whsOperateLog.setRemark(remarks);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /**
     * 4.自管仓 备件货 按箱 出库 减少仓库库存 - 日志
     */
    @Override
    public void insertWhsOperateLogBC(String boxNote, double quantity, String whsOutUuid, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("1");
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("4");
        whsOperateLog.setRelateDocumentNumber(whsOutUuid);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /**
     * 6.自管仓 贸易货 按物料 下架 减少储位库存 - 日志
     */
    @Override
    public void insertWhsOperateLogMX(String uuidStorage, String materialNo, double quantity, String whsOutUuid, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setMaterialNo(materialNo);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("3");
        whsOperateLog.setRelateDocumentNumber(whsOutUuid);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    @Override
    public List<WhsOperateLog> getWhsOperateLog(String uuid) {
        if (StringUtils.isNotEmpty(uuid)) {
            return this.baseMapper.getWhsOperateLog(uuid);
        }
        return null;
    }

    /**
     * 7.自管仓 备件货 按箱 上架 增加储位库存 - 日志
     */
    @Override
    public void insertWhsOperateLogBI(String uuidStorage, String boxNote, double quantity, String documentNumber, String comments, String userId, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("2");
        whsOperateLog.setRelateDocumentNumber(documentNumber);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    @Override
    public void insertWhsOperateLogBI(String uuidStorage, String boxNote, double quantity, String documentNumber, String comments, String userId, String remarks, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("2");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType("2");
        whsOperateLog.setRelateDocumentNumber(documentNumber);
        whsOperateLog.setComments(comments);
        whsOperateLog.setRemark(remarks);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    /**
     * {
     * "werks":"CN01",
     * "whsLocationCode":"1061",
     * "documentType":"1", //1自管仓仓库库存日志；2自管仓储位库存日志；3海外仓仓库库存日志；4海外仓储位库存日志；
     * "storageNo":"A010101",
     * "materialNo":"2019192",
     * "boxNote":"BJ-BOXNOMBER",
     * "operationType":"1", //1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)；
     * "createTime":["2021-04-01","2021-05-01"],
     * "userId":"999904",
     * }
     * */
    @Override
    public ResponseResult getWhsDeskAccount(JSONObject filterCriteria) {
        try {
            String werks = filterCriteria.getString("werks");
            String whsLocationCode = filterCriteria.getString("whsLocationCode");
            String documentType = filterCriteria.getString("documentType");
            String storageNo = filterCriteria.getString("storageNo");
            String materialNo = filterCriteria.getString("materialNo");
            String boxNote = filterCriteria.getString("boxNote");
            String operationType = filterCriteria.getString("operationType");
            String userId = filterCriteria.getString("userId");
            String logCategory = filterCriteria.getString("logCategory");
            werks = StringUtils.isNotEmpty(werks) ? werks : null;
            whsLocationCode = StringUtils.isNotEmpty(whsLocationCode) ? whsLocationCode : null;
            documentType = StringUtils.isNotEmpty(documentType) ? documentType : null;
            storageNo = StringUtils.isNotEmpty(storageNo) ? storageNo : null;
            materialNo = StringUtils.isNotEmpty(materialNo) ? materialNo : null;
            boxNote = StringUtils.isNotEmpty(boxNote) ? boxNote : null;
            operationType = StringUtils.isNotEmpty(operationType) ? operationType : null;
            userId = StringUtils.isNotEmpty(userId) ? userId : null;
            String queryBox = null, queryMaterial = null;
            if (null == logCategory) {

            } else if (logCategory.equals("M")) {
                queryMaterial = logCategory;
            } else if (logCategory.equals("X")) {
                queryBox = logCategory;
            }
            JSONArray createTime = filterCriteria.containsKey("createTime") ? filterCriteria.getJSONArray("createTime") : null;
            String startTime = null, endTime = null;
            if (null != createTime && createTime.size() > 0) {
                startTime = createTime.getString(0);
                endTime = createTime.getString(1);
            }
            return ResponseResult.success().add("whsOperateLog",
                    this.baseMapper.getWhsDeskAccount(werks, whsLocationCode, storageNo, materialNo, boxNote,
                            documentType, operationType, startTime, endTime, userId, queryBox, queryMaterial));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("Failed when query OperateLog");
        }

    }

    @Override
    public ResponseResult getWhsDeskAccountPage(JSONObject filterCriteria) {
        try {
            String werks = filterCriteria.getString("werks");
            String whsLocationCode = filterCriteria.getString("whsLocationCode");
            String documentType = filterCriteria.getString("documentType");
            String storageNo = filterCriteria.getString("storageNo");
            String materialNo = filterCriteria.getString("materialNo");
            String boxNote = filterCriteria.getString("boxNote");
            String operationType = filterCriteria.getString("operationType");
            String userId = filterCriteria.getString("userId");
            String logCategory = filterCriteria.getString("logCategory");
            werks = StringUtils.isNotEmpty(werks) ? werks : null;
            whsLocationCode = StringUtils.isNotEmpty(whsLocationCode) ? whsLocationCode : null;
            documentType = StringUtils.isNotEmpty(documentType) ? documentType : null;
            storageNo = StringUtils.isNotEmpty(storageNo) ? storageNo : null;
            materialNo = StringUtils.isNotEmpty(materialNo) ? materialNo : null;
            boxNote = StringUtils.isNotEmpty(boxNote) ? boxNote : null;
            operationType = StringUtils.isNotEmpty(operationType) ? operationType : null;
            userId = StringUtils.isNotEmpty(userId) ? userId : null;
            String queryBox = null, queryMaterial = null;

            int pageNo = StringUtils.isEmpty(filterCriteria.getString("pageNo")) ? 1 : filterCriteria.getInteger("pageNo");
            int pageSize = StringUtils.isEmpty(filterCriteria.getString("pageSize")) ? 20 : filterCriteria.getInteger("pageSize");

            if (null == logCategory) {

            } else if (logCategory.equals("M")) {
                queryMaterial = logCategory;
            } else if (logCategory.equals("X")) {
                queryBox = logCategory;
            }
            JSONArray createTime = filterCriteria.containsKey("createTime") ? filterCriteria.getJSONArray("createTime") : null;
            String startTime = null, endTime = null;
            if (null != createTime && createTime.size() > 0) {
                startTime = createTime.getString(0);
                endTime = createTime.getString(1);
            }
            Page<Map<String, Object>> pages = new Page<>(pageNo, pageSize);
            List<Map<String, Object>> pageData = this.baseMapper.getWhsDeskAccountPage(pages, werks, whsLocationCode, storageNo, materialNo, boxNote,
                    documentType, operationType, startTime, endTime, userId, queryBox, queryMaterial);
            pages.setRecords(pageData);
            return ResponseResult.success().add("whsOperateLog", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("Failed when query OperateLog");
        }
    }

    @Override
    public void insertWhsOperateLog(String documentType, String uuidStorage, String materialNo, String boxNote,
                                    double quantity, String operateType, String relateDocumentNumber, String comments,
                                    String userId, String remark, String werks, String whsLocationCode) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType(documentType);
        if (StringUtils.isNotEmpty(uuidStorage)){
            whsOperateLog.setUuidStorage(uuidStorage);
        }
        if (StringUtils.isNotEmpty(materialNo)){
            whsOperateLog.setMaterialNo(materialNo);
        }
        if (StringUtils.isNotEmpty(boxNote)){
            whsOperateLog.setBoxNote(boxNote);
        }
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType(operateType);
        whsOperateLog.setRelateDocumentNumber(relateDocumentNumber);
        whsOperateLog.setComments(comments);
        if (StringUtils.isNotEmpty(remark)){
            whsOperateLog.setRemark(remark);
        }
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        this.baseMapper.insert(whsOperateLog);
    }

    @Override
    public void insertOperateLog(String documentType, String uuidStorage, String materialNo, String boxNote,
                                    double quantity, String operateType, String relateDocumentNumber, String comments,
                                    String userId, String remark, String werks, String whsLocationCode,String deliveryNoteSap) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType(documentType);
        if (StringUtils.isNotEmpty(uuidStorage)){
            whsOperateLog.setUuidStorage(uuidStorage);
        }
        if (StringUtils.isNotEmpty(materialNo)){
            whsOperateLog.setMaterialNo(materialNo);
        }
        if (StringUtils.isNotEmpty(boxNote)){
            whsOperateLog.setBoxNote(boxNote);
        }
        whsOperateLog.setQuantity(quantity);
        /** 1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)； */
        whsOperateLog.setOperationType(operateType);
        whsOperateLog.setRelateDocumentNumber(relateDocumentNumber);
        whsOperateLog.setComments(comments);
        if (StringUtils.isNotEmpty(remark)){
            whsOperateLog.setRemark(remark);
        }
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        if(StringUtils.isNotEmpty(deliveryNoteSap)){
            whsOperateLog.setDeliveryNoteSap(deliveryNoteSap);
        }
        this.baseMapper.insert(whsOperateLog);
    }
}
